package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.vnp.cooking.R;
import com.vnp.cooking.UIcontrols.MyCircleImageView;
import com.vnp.cooking.bean.ObjThongbao;
import com.vnp.cooking.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListThongbaoAdapter extends BaseAdapter {
	Context context;
	ArrayList<ObjThongbao> objThongbaos;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565).build();
	private int lastPosition = -1;

	public ListThongbaoAdapter(Context context,
			ArrayList<ObjThongbao> objThongbaos) {
		super();
		this.context = context;
		this.objThongbaos = objThongbaos;
	}

	@Override
	public int getCount() {
		return objThongbaos.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			v = inflater.inflate(R.layout.list_noti_item, parent, false);

			holder = new ViewHolder();

			holder.imgTBAvatar = (MyCircleImageView) v
					.findViewById(R.id.img_avata_TB);
			holder.txtContentTB = (TextView) v
					.findViewById(R.id.txt_content_TB);
			holder.timeAgoTB = (TextView) v.findViewById(R.id.txt_timeago_TB);

			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		// set gia tri
		if (objThongbaos.get(position).getAvataChef().equals("https://id.vatgia.com/avatar/512/000/702/e4.png")) {
			holder.imgTBAvatar.setImageResource(R.drawable.img);
		} else {
			ImageLoader.getInstance().displayImage(
					objThongbaos.get(position).getAvataChef(), holder.imgTBAvatar,
					options);
		}
		
		String timeAgo = Utils.getTimeAgoAsString(Utils.getCurrentTime() - Utils.convertStringToLong(objThongbaos.get(position).getCreated()));
		holder.timeAgoTB.setText(timeAgo);
		
		String name = objThongbaos.get(position).getNameChef();
		
		String type = getTypeAction(objThongbaos.get(position).getTypeThongbao());
		String nameRecipe = objThongbaos.get(position).getNameRecipe();
		
		SpannableString nameBold = new SpannableString(name);
		nameBold.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), 0);
		
		String rsbase = context.getResources().getString(R.string.content);
		String rs = String.format(rsbase , Html.fromHtml("<b>" + name + "</b>"), type, nameRecipe );
		
//		String rs = objThongbaos.get(position).getNameChef()+" đã " + typeColor + " công thức " + nameRecipeColor;
		
		if (rs.length() > 70) {
			holder.txtContentTB.setText(rs.substring(0, 70)+ "...");
		} else {
			holder.txtContentTB.setText(rs);
		}
		/*holder.txtContentTB.setText(objThongbaos.get(position)
				.getContenThongbao());*/
		/*holder.timeAgoTB.setText(""
				+ objThongbaos.get(position).getTimeAction() + " phút");*/

		/*// Animation list
		Animation animation = AnimationUtils.loadAnimation(context,R.anim.listview_push_up_out);
		v.startAnimation(animation);*/
		return v;
	}

	class ViewHolder {
		MyCircleImageView imgTBAvatar;
		TextView txtContentTB;
		TextView timeAgoTB;
	}

	@Override
	public Object getItem(int position) {
		return objThongbaos.get(position);
	}
	
	String getTypeAction(String type) {
		String rs = null;
		if (type.equals("like")) {
			rs = "thích";
		} else if (type.equals("comment")) {
			rs = "bình luận";
		}
		return rs;
		
	}
	

}
